package org.dictionary;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

/**
 *
 * @author siarhei
 */
public class DictionaryActivity extends ListActivity {

    private String searchPattern = "";


    private final static int ACTIVITY_CREATE = 0;
    private final static int ACTIVITY_EDIT = 1;
    private final static int ACTIVITY_VIEW = 2;

    private Cursor dictCursor;
    private DictionaryDao dictDao;

    public DictionaryActivity() {
        this.dictDao = DictionaryDao.getInstance(this);
    }

    @Override
    public void onCreate(Bundle icicle) {
        Log.d(getClass().getSimpleName(), "onCreate()");
        super.onCreate(icicle);
        setContentView(R.layout.main);
        
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            searchPattern = intent.getStringExtra(SearchManager.QUERY);
        }

        updateList();

        registerForContextMenu(getListView());

        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Log.d(getClass().getSimpleName(), intent.getData().toString());
            Long id = Long.parseLong(intent.getData().getLastPathSegment());
            viewEntry(id);
        }
    }

    private void updateList() {
        // if the search pattern has been changed we create a new cursor and release the old one
        if (dictCursor != null) {
            stopManagingCursor(dictCursor);
            dictCursor.close();
        }
        dictCursor = dictDao.getCursor(searchPattern);
        startManagingCursor(dictCursor);
        String[] from = new String[]{DictionaryEntry.ORIGINAL_TEXT};
        int[] to = new int[]{R.id.text1};
        SimpleCursorAdapter entries = new SimpleCursorAdapter(this, R.layout.list_row, dictCursor, from, to);
        setListAdapter(entries);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        viewEntry(id);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("More");
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent i;
        switch (item.getItemId()) {
            case R.id.search:
                onSearchRequested();
                return true;

            case R.id.add:
                i = new Intent(this, EntryEditActivity.class);
                startActivityForResult(i, ACTIVITY_CREATE);
                return true;

            case R.id.all:
                searchPattern = "";
                updateList();
                return true;

            /*case R.id.quiz:
                i = new Intent(this, QuizMainActivity.class);
                startActivityForResult(i, ACTIVITY_CREATE);
                return true;*/

            default:
                return false;
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case R.id.delete:
                dictDao.delete(info.id);
                updateList();
                return true;

            case R.id.edit:
                Intent i = new Intent(this, EntryEditActivity.class);
                i.putExtra(DictionaryEntry._ID, info.id);
                startActivityForResult(i, ACTIVITY_EDIT);
                return true;

            default:
                return super.onContextItemSelected(item);
      }
    }

    // navigation
    private void viewEntry(long id) {
        Intent i = new Intent(this, EntryViewActivity.class);
        i.putExtra(DictionaryEntry._ID, id);
        startActivityForResult(i, ACTIVITY_VIEW);
    }

}
